<%@ page import="java.util.LinkedList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ page import="javax.xml.parsers.DocumentBuilderFactory,javax.xml.parsers.DocumentBuilder,
				 org.w3c.dom.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>問卷</title>
<script language="Javascript" type="text/javascript">
//檢查有無沒填的題目
function confirm(idnums){
	var i=1,count=0,flag=0;
	while(i <= idnums){
		count++;
		var obj = document.getElementById("radio"+i);
		if(obj.checked == false){
			flag++;
		}
		if(count == 5){
			if(flag == 5){
				alert("還有測驗尚未填選喔");
				obj.focus();
				return;
			}
			count=0;
			flag=0;
		}
		i++;
	}
	form1.submit();
}
//避免同樣題目勾選不同答案的情形
function check(Qnums,types){
	var num=Qnums;
	var type = new Array("A","B","C","D","E","F","G","H","I");
	while(true){
		for(var count=0;count<types;count++){
			for(var j=1;j<=num;j++){
				var obj = document.getElementsByName("type"+type[count]+j);
				if(obj.length > 0){
					for(var k=0;k<obj.length;k++){
						if(obj[k].checked){ 
							for(var count1=count+1;count1<type.length;count1++){
								var obj1 = document.getElementsByName("type"+type[count1]+j);
								if(obj1.length > 0 && !obj1[k].checked){
									obj1[k].checked=true;
								}
							}
							break;
						}
					}
				}
			}
		}
		break;
	}
	
}
</script>

<style type="text/css">
<!--
body {
	background-image: url(img/grass.jpg);
    background-position: center top;
	background-color: #FFFFFF;
	background-repeat: repeat;
}
body,td,th {
	color: #000000;
	font-size:24px;
	font-family: Arial;
}


a:link {
font-family:"標楷體"; 
	font-size:25px;
	text-decoration:none;
	color:#277C35;
	background-color:#FFFFFF;
}
a:visited {
	font-family:"標楷體"; 
	font-size:25px;
	text-decoration:none;
	color:#277C35;
	background-color:#FFFFFF;
}
a:hover {
font-family:"標楷體"; 
	font-size:25px;
	text-decoration:none;
	color:#000000;
	background-color:#FFFFFF;
}


.ind:link {
color: #535353;
text-decoration:none;
font-family:"標楷體";
font-size:18px;
}
.ind:visited {
color:#535353;
text-decoration:none;
font-family:"標楷體";
font-size:18px;
}

.ind:hover {
color:#BB280F;
text-decoration:none;
font-family:"標楷體";
font-size:18px;
}


.HEAD_STYLE {
	font-weight:bold;
	color: #1B4001;
	font-family:"標楷體";
	font-size:30px; 
}
.TEXT_STYLE {
	font-family:Arial;
	color:#193940;
	font-size:24px;
	font-weight:bold;
}
.BOTTOM_STYLE {
	color: #FFFFFF; 
	font-family: Arial; 
	font-size:20px;
}
.LEFT_BUTTON {
	font-family:"標楷體"; 
	font-size:30px;
}

-->
</style>

</head>
<body alink="#FF0000" vlink="#FF0000" onLoad="KW_doClock()">
<table width="100%" border="0">
		<tr>
			<td><p>&nbsp;</p></td>
		</tr>
				
			
			
		
 
    		<td width="5%">&nbsp;</td>
			
			<td bgcolor="#FFFFFF"><table width="100%" border="0">
      			<tr>
        			<td background="img/herbal.jpg">
						<p>&nbsp;</p>
          				<p>&nbsp;</p>
          				<p>&nbsp;</p>
          				<p>&nbsp;</p>
		  				<p>&nbsp;</p>
          
          			</td>
			  </tr>
     
	 
	 
	 
	  			  <tr>
        			<td>	
						<table bgcolor="#437A27" width="100%" border="0">
         					 <tr>
            					<td>&nbsp;</td>
           			 			
          			 		</tr>
        				</table>
					</td>
     			  </tr>
      
	  			
	  
	  
	  
	 			  <tr>
        				<td>
							<table width="100%">
           						<tr>
									<td valign="top">
										<table width="100%">
											
											<tr><td><table width="100%"><tr>
												<td width="15%"></td>
												<td  align="center" class="HEAD_STYLE">中醫體質判定自測表</td>
												<td width="15%">
												<div align="right">
              <script language='JavaScript'>
// Kaosweaver Live Clock Start
function class_clock(f,s,c,b,w,h,d,m,g,z) { // Copyright 2002 by Kaosweaver, All rights reserved
	this.b=b;this.w=w;this.h=h;this.d=d;this.g=g;this.z=z
	this.o='<font style="color:'+c+'; font-family:'+f+'; font-size:'+s+'pt;">';
if (m==1) this.o+=0
}
var clock=new class_clock("Arial, Helvetica, sans-serif","20","#437A27","#FFFFFF","140",0,1,0,0,0)
// If the clock's size needs adjusting, change the 140 above.
d=document
if (d.all || d.getElementById) {d.write('<span id="activeClock" style="width:'+clock.w+'px; background-color:'+clock.b+'"></span>'); }
else if (d.layers) {d.write('<ilayer bgcolor="'+clock.b+'" id="wrapClock"><layer width="'+clock.w+'" id="activeClock"></layer></ilayer>'); }
else {KW_doClock(1);}
function KW_doClock(a) { // Copyright 2003 by Kaosweaver, All rights reserved
	d=document;t=new Date();p="";dClock="";	if (d.layers) d.wrapClock.visibility="show";
	tD=(t.getTimezoneOffset()-(clock.z*60))*clock.g;t.setMinutes(tD+t.getMinutes())
	h=t.getHours();m=t.getMinutes();s=t.getSeconds();if (clock.h)
	 {p=(h>11)?"PM":"AM";h=(h>12)?h-12:h;h=(h==0)?12:h;}if (clock.d)
	 {m=(m<=9)?"0"+m:m; s=(s<=9)?"0"+s:s;} dClock = clock.o+h+':'+m+':'+s+' '+p+'</font>';
	if (a) {d.write(dClock);}if (d.layers) {wc = document.wrapClock;lc = wc.document.activeClock;
		lc.document.write(dClock);lc.document.close();
	} else if (d.all) {	activeClock.innerHTML = dClock;
	} else if (d.getElementById) {d.getElementById("activeClock").innerHTML = dClock;}
	if (!a) setTimeout("KW_doClock()",1000);
}

// Kaosweaver Live Clock End
              </script>
              <!-- KW Live Clock -->
</div>
												
												
												</td>
												</tr></table></td>	
											</tr>
											
											
								
											
											<tr>
												<td><form action="caltest.jsp" method="post" name="form1">
<%
	String category = request.getParameter("category");
	String path;
	if(category.equals("A")){
		path = "./WEB-INF/tcm.xml";
	}
	else{
		path = "./WEB-INF/tcm2.xml";
	}
	String File = request.getRealPath(path);
	Document BookDom = null;
	DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	
	try{
		DocumentBuilder builder = factory.newDocumentBuilder();
		BookDom = builder.parse(File);
		
	}catch(Exception e){
		out.print("XML analysis error" + e.getMessage());
	}
			
%>
<%!
	public boolean isTextNode(Node node){
		return node.getNodeName().equals("#text");
}
%>
<%  //讀取xml
	int questionflag=0,marklistflag=0;
	LinkedList<String> questions = new LinkedList<String>(); //放題目
	LinkedList<String> marklists = new LinkedList<String>(); //放題目填答的等級
	LinkedList<String> CLASS = new LinkedList<String>();//放型別(為了show哪種型別是對應哪些題目用)
	LinkedList<String> CLASSforQ = new LinkedList<String>();//放根據型別的題目(與CLASS做對照)
	
	Element element = BookDom.getDocumentElement();
	NodeList nodes1 = element.getChildNodes();
	for(int i=0;i < nodes1.getLength() ; i++){
		Node temp1 = nodes1.item(i);
		NamedNodeMap attrlist1 = temp1.getAttributes();
		if(attrlist1 != null)
		{
			//System.out.println(attrlist1.item(0).getNodeName());
			//System.out.println(attrlist1.item(0).getNodeValue());
		}
		if(isTextNode(temp1)){
			continue;
		}
		if(temp1.getNodeName() == "QUESTION"){
			questionflag=1;
		}
		if(temp1.getNodeName() == "MARKLIST"){
			marklistflag=1;
		}
		NodeList nodes2 = temp1.getChildNodes();
		for(int j=0;j < nodes2.getLength();j++){
			Node temp2 = nodes2.item(j);
			NamedNodeMap attrlist2 = temp2.getAttributes();
			if(attrlist2 != null && temp2.getNodeName() == "CLASS"){
				CLASS.add(attrlist2.item(0).getNodeValue());
				//System.out.println(attrlist2.item(0).getNodeName());
				//System.out.println(attrlist2.item(0).getNodeValue());
			}

			if(isTextNode(temp2)){
				continue;
			}
			if(questionflag == 1){
				questions.add(temp2.getFirstChild().getNodeValue());
			}
			if(marklistflag == 1){
				marklists.add(temp2.getFirstChild().getNodeValue());
				//System.out.println(marklists.peekLast());
			}
			if(temp2.hasChildNodes() && temp2.getNodeName() == "CLASS"){
				NodeList nodes3 = temp2.getChildNodes();
				for(int k=0; k<nodes3.getLength();k++){
					Node temp3 = nodes3.item(k);
					if(isTextNode(temp3)){
						continue;
					}
					CLASSforQ.add(temp3.getNodeName());
					//System.out.println(":"+CLASSforQ.peekLast());
				}
				CLASSforQ.add("end");
				//System.out.println(":"+CLASSforQ.peekLast());
			}

		}
		questionflag = 0;
		marklistflag = 0;
	}
	//將讀進來的體質由中文轉變成英文(供radio按鈕命名用)
	String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	LinkedList<String> CLASStoEng = new LinkedList<String>();//放型別(英文型式)
	for(int i=0 ; i < CLASS.size();i++){
		CLASStoEng.add(alphabet.substring(i, i+1));
	}
	
	
%>
<%
	int j=0,count=0,levels=0,id=0,count1=0;
	for(int i=0;i<CLASS.size();i++){
%>
		<center><font size="5" style="font: bold normal bold 18pt/30pt '標楷體'" color="66CC33"><%= CLASS.get(i) %></font></center>
		<br>
		<table align="center" border="2" width="700">
				<tr bgcolor="FFFF99">
					<td style="color:#193940;">請根據近一年的體驗和感覺,回答下列問題</td>
<%
		while(levels < marklists.size()){
%>
					<td align="center"><%= marklists.get(levels) %></td>
<%
			levels++;
		}
%>
				</tr>
<% 				
		while(j < CLASSforQ.size()){
			if(CLASSforQ.get(j).equals("end")){				
				break;
			}
			String ques = CLASSforQ.get(j).substring(1);
%>
			<tr><td width="450"><%= "("+(count+1)+")" %><%= questions.get((Integer.parseInt(ques))-1) %></td>
				<td><input type="radio" name="type<%=CLASStoEng.get(i)%><%= ques%>" id="radio<%= (id+1)%>" value="1" onClick="check(<%= questions.size() %>,<%= CLASS.size() %>)"/><% id++; %>
				  <span class="TEXT_STYLE">1</span></td>
				<td><input type="radio" name="type<%=CLASStoEng.get(i)%><%= ques%>" id="radio<%= (id+1)%>" value="2" onClick="check(<%= questions.size() %>,<%= CLASS.size() %>)"/><% id++; %>
				  <span class="TEXT_STYLE">2</span></td>
				<td><input type="radio" name="type<%=CLASStoEng.get(i)%><%= ques%>" id="radio<%= (id+1)%>" value="3" onClick="check(<%= questions.size() %>,<%= CLASS.size() %>)"checked="checked"/><% id++; %>
				  <span class="TEXT_STYLE">3</span></td>
				<td><input type="radio" name="type<%=CLASStoEng.get(i)%><%= ques%>" id="radio<%= (id+1)%>" value="4" onClick="check(<%= questions.size() %>,<%= CLASS.size() %>)"/><% id++; %>
				  <span class="TEXT_STYLE">4</span></td>
				<td><input type="radio" name="type<%=CLASStoEng.get(i)%><%= ques%>" id="radio<%= (id+1)%>" value="5" onClick="check(<%= questions.size() %>,<%= CLASS.size() %>)"/><% id++; %><% count++; %>
				  <span class="TEXT_STYLE">5</span></td>
			</tr>
<%
			j++;
		}
%>
		</table>
		<br>
<%
		j++;
		count=0;
		levels=0;
	}
%>
<input type="hidden" name="category" value="<%= category %>"> <!--傳題目類型--> 
<input type="hidden" name="quesCount" value="<%= questions.size() %>"> <!--傳共有多少種題目-->
<input type="hidden" name="typeCount" value="<%= CLASS.size() %>"> <!--傳有多少種分類-->
<center><input type="button" value="我填好了!!" onClick="confirm(<%= id %>)"/></center>
</form>
</td>
											</tr>
											
											
										</table>
									
             

		
											
								  </td>
									

								</tr>
       					 </table>
         

         				</td>
			  </tr>
   
	  
					 <tr>
      					  
   					    <td><p>&nbsp;</p></td>
  					 </tr>
	  				
					<tr>
      					  <td bgcolor="#437A27"><div align="center" class="BOTTOM_STYLE">Copyright@CCU_PLlab</div></td>
   
			  </tr>
    						
  	 			  
	
			</table>
		</td>
	    
		<td width="5%">&nbsp;</td>
</table>
<p>&nbsp;</p>
</body>
</html>
